

$(document).ready(function () {
    init( callback, 1);
});


function init( callback, current) {
    $("#thislist").html("");
    $.ajax({
        type: "get",
        url: urlcore + "/api/experienceGoods/selectPage?current=" + current ,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
            if (data.success == true) {
                callback(data.data.records);
                $('#thiscount').text(data.data.total);
                $("#pager").pager({
                    pagenumber: current,
                    pagecount: data.data.pages,
                    totalcount: data.data.total,
                    buttonClickCallback: PageClick
                });
            } else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error: function () {
            alert("查询失败！");
        }
    });

}


//回调函数
PageClick = function (pageclickednumber) {
    init( callback,pageclickednumber);
}

var callback = function (data) {
    var html = "", goodsName = "", goodsType = "", goodsUrl = "", markerPrice = "", returnRate = "", returnRateY = "",
        goodsStatus = "", gmtDatetime = "", id = "";
    var goodsStatusShow = "";//显示的商品状态
    var changgeStatusOptBtn = "";
    var gId = "";
    var picUrl = "";
    for (var i = 0; i < data.length; i++) {
        id = data[i].id;
        goodsName = data[i].goods.goodsName;
        goodsUrl = data[i].goods.goodsUrl != null ? data[i].goods.goodsUrl : "";
        picUrl =data[i].picUrl != null ? data[i].picUrl:"";
        mPrice = data[i].goods.mPrice;
        returnRate = data[i].goods.returnRate;
        postage = data[i].goods.postage;
        gId = data[i].goodsId;
        /*邮费类型（1 江浙沪包邮 2  满九十九包邮 3  七块钱邮费  4 全国包邮（港澳台地区、西藏、新疆、内蒙古、宁夏、青海等地区除外））*/
 /*       if (postage == undefined) {
            postage = "";
        } else if (postage == 1) {
            postage = "江浙沪包邮";
        } else if (postage == 2) {
            postage = "满88包邮";
        } else if (postage == 3) {
            postage = "5块钱邮费"
        } else if (postage == 4) {
            postage = "全国包邮（港澳台地区、西藏、新疆、内蒙古、宁夏、青海等地区除外）";
        }*/
        returnRateY = data[i].goods.returnRatey;
        goodsStatus = data[i].goods.goodsStatus;
        if (data[i].goods.goodsType == 0) {
            goodsType = "蜜哆哆";
        } else if (data[i].goods.goodsType == 1) {
            goodsType = "蜜雅阁";
        } else if (data[i].goods.goodsType == 2) {
            goodsType = "品牌馆";
        } else {
            goodsType = "";
        }

        var price = data[i].price != null ? data[i].price : "";
        var num = data[i].num != null ? data[i].num : "";

        if (goodsStatus == 1) {
            goodsStatusShow = "尚未发布";
            changgeStatusOptBtn = "   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"changgeToStatus(" + id + "," + "2" + ")\"> 发布上架</a>\n";
        } else if (goodsStatus == 2) {
            goodsStatusShow = "已发布上架";
            changgeStatusOptBtn = "   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"changgeToStatus(" + id + "," + "3" + ")\"> 下架</a>\n";

        } else if (goodsStatus == 3) {
            goodsStatusShow = "已下架";
            changgeStatusOptBtn = "   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"changgeToStatus(" + id + "," + "2" + ")\"> 重新上架</a>\n";
        } else {
            goodsStatusShow = "";
        }
        html += "  <tr id=\"" + id + "\">\n" +
            "  \t\t<td>" + (i + 1) + "</td>\n" +
            "  \t\t<td>" + goodsName + "</td>\n" +
            "  \t\t<td>" + price + "</td>\n" +
            "  \t\t<th>" + num + "</th>\n" +
            "\t\t <td class=\"footable-visible\"><img width=\"auto\" height=\"50px\" src=\"" + picUrl + "\"></td>\n" +
            //"  \t\t<th>" + returnRate + "</th>\n" +
            // "  \t\t<th>" + postage + "</th>\n" +
            "  \t\t<th>" + goodsType + "</th>\n" +
            "  \t\t<th>" + goodsStatusShow + "</th>\n" +
            "  \t\t<th>" + gmtDatetime + "</th>\n" +
            "  \t\t<td>\n" +
        // data-toggle="modal" data-target="#myModal"
            "  \t\t\t<a class=\"btn btn-info btn-xs\" href=\"javascript:;\"  onclick=\"view(" + data[i].goodsId + ")\"> 问卷详细</a>\n" +
            "  \t\t\t<a class=\"btn btn-danger btn-xs\" href=\"javascript:;\"  onclick=\"del(" + data[i].goodsId + ")\"> 删除问卷</a>\n" +
            "  \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"selectques(" + data[i].goodsId + ")\"> 选择问卷</a>\n" +
            "  \t\t</td>\n" +
            "  \t</tr>";
    }
    $("#thislist").append(html);
}


function selectques(id) {
    window.location.href = "exp_question_add.html?id="+id;
}
//详细
function view(id) {
    $.ajax({
        type: "GET",
        url: urlcore + "/api/goodsQuerstion/getGoodsQuesList?id=" + id,
        dataType: "json",
        headers: {"Content-Type": "application/json;charset=UTF-8"},
        data: {},
        success: function (data) {

        if(data.success){
            $("#myModal").modal();
            var html = "<tr>\n" +
                "<td>问题</td>\n" +
                "<td>类型</td>\n" +
                // "<td>操作</td>\n" +
                "</tr>";
            var ques = data.data.list;
            $("#myModalLabel").html(data.data.naire.title);//标题
            $.each(ques, function(i,v){
                var thrHtml = "";
                $.each(v.option, function(a,val){
                    var bfb;//统计百分数
                    if(val.count == 0){
                        bfb = 0.00;
                    }else{
                        bfb = (val.opsCount/val.count).toFixed(2);
                    }
                    thrHtml += "<td>"+val.options+"、"+val.content+"&nbsp;&nbsp;（"+bfb+"%）</td>";
                });
                var lex = "";
                if(v.type == 1){
                    lex = "单选";
                }else{
                    lex = "多选";
                }
                html += "<tr>\n" +
                    "<td>"+(i+1)+"、"+v.title+"</td>\n" +
                    "<td>"+lex+"</td>\n" +
                    "</tr>\n" +
                    "<tr>\n" + thrHtml+
                    "</tr>";



            });
            $("#opTables").html(html);
        }else{
            // $("#opTables").html("");
            alert(data.msg)
        }
        },
        error: function () {
            alert("数据异常");
        }
    });
}

//删除

 function del(id) {
     if(confirm("删除后将不可恢复")){
         $.ajax({
             type: "GET",
             url: urlcore + "/api/goodsQuerstion/deleteExGoods?id=" + id,
             dataType: "json",
             headers: {"Content-Type": "application/json;charset=UTF-8"},
             data: {},
             success: function (data) {//data是返回的hash,key之类的值，key是定义的文件名
                 if (data.success == true) {
                     location.reload();
                 } else {
                     alert("该商品下没有问卷");
                 }
             },
             error: function () {
                 alert("删除失败");
             }
         });
     }

}


//用户答卷 测试
function yhdj() {

var userQuerstion = [];
    userQuerstion.push({
        userId:1,
        goodsId:11,
        querstionId:18,
        optionId:58
    });
    var jso = JSON.stringify(userQuerstion);
        $.ajax({
            type: "POST",
            url: urlcore + "/api/goodsQuerstion/saveUserQues",
            dataType: "json",
            headers: {"Content-Type": "application/json;charset=UTF-8"},
            data: JSON.stringify(userQuerstion),
            success: function (data) {
            },
            error: function () {
                alert("数据异常");
            }
        });
}


//搜索
function searchuser() {
    var type = $('#goods_type option:selected').val();//选中的值
    init(type, callback, 1);
}


